######################################
#### table_1.R: Generates Table 1 ####
######################################

dta_accuracy$endtime <- as.Date(dta_accuracy$endtime)
dta_accuracy$pre <- "pre"

dta_accuracy$weekfrom <- "Before"
for (i in 1:nrow(dta_accuracy)){
  if(as.Date(dta_accuracy$endtime[i]) > as.Date("2022-11-08") & as.Date(dta_accuracy$endtime[i]) <= as.Date("2022-11-08") +7){
    dta_accuracy$weekfrom[i] <- "Week1"
  }
  if(as.Date(dta_accuracy$endtime[i]) > as.Date("2022-11-08") +7 & as.Date(dta_accuracy$endtime[i]) <= as.Date("2022-11-08") +14){
    dta_accuracy$weekfrom[i] <- "Week2"
  }
  if(as.Date(dta_accuracy$endtime[i]) > as.Date("2022-11-08") +14 & as.Date(dta_accuracy$endtime[i]) <= as.Date("2022-11-08") +21){
    dta_accuracy$weekfrom[i] <- "Week3"
  }
  if(as.Date(dta_accuracy$endtime[i]) > as.Date("2022-11-08") +21){
    dta_accuracy$weekfrom[i] <- "Week4"
  }
}

dta_accuracy$pre[dta_accuracy$endtime > as.Date("2022-11-08")] <- "post"

dta_accuracy$accuracyre <- 0
dta_accuracy$accuracyre[dta_accuracy$accuracy <3] <- 1

dta_accuracy$days <- as.Date(dta_accuracy$endtime) - as.Date("2022-11-08")

dta_accuracy$pre <- as.factor(dta_accuracy$pre)
dta_accuracy$pre <- relevel(dta_accuracy$pre,"pre")

accuracy_overall <- lm(accuracyre~pre*days, weights=weight,data=dta_accuracy)
accuracy_dem <- lm(accuracyre~pre*days, weights=weight,data=dta_accuracy[dta_accuracy$pid == "Democrat",])
accuracy_rep <- lm(accuracyre~pre*days, weights=weight,data=dta_accuracy[dta_accuracy$pid == "Republican",])
accuracy_ind <- lm(accuracyre~pre*days, weights=weight,data=dta_accuracy[dta_accuracy$pid == "Independent",])

texreg(list(accuracy_overall, accuracy_dem, accuracy_ind, accuracy_rep), digits = 2, include.ci = TRUE, 
       stars = c(0.001, 0.01, 0.05),
       custom.model.names = c("All respondents", "Democrats", "Independents", "Republicans"),
       caption.above = TRUE,
       custom.note = "*$p<.05$.",
       custom.coef.names= c( "Intercept","Post Election", "Days from election", "Post Election X Days from election"))

accuracy_overall <- lm(accuracyre~weekfrom, weights=weight,data=dta_accuracy)
accuracy_dem <- lm(accuracyre~weekfrom, weights=weight,data=dta_accuracy[dta_accuracy$pid == "Democrat",])
accuracy_rep <- lm(accuracyre~weekfrom, weights=weight,data=dta_accuracy[dta_accuracy$pid == "Republican",])
accuracy_ind <- lm(accuracyre~weekfrom, weights=weight,data=dta_accuracy[dta_accuracy$pid == "Independent",])


texreg(list(accuracy_overall, accuracy_dem, accuracy_ind, accuracy_rep), digits = 2, include.ci = TRUE, 
       stars = c(0.001, 0.01, 0.05),
       custom.model.names = c("All respondents", "Democrats", "Independents", "Republicans"),
       caption.above = TRUE,
       custom.note = "*$p<.05$.",
       custom.coef.names= c( "Intercept","Week 1 Post Election", "Week 2 Post Election", "Week 3 Post Election", "Week 4 Post Election"))
